import { JSEncrypt } from 'jsencrypt'

// 密钥对生成 http://web.chacuo.net/netrsakeypair

const publicKey =
`
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCjkcuq31iyZnGA5NgcGcrBWyqe
0ycOvbuSG+nz8KjdwOHGZTFCQZE9lk8f9DmLXT1NPukWqIrtQVrr/ZQolUedP1RV
PaxwKTEhucpR1EYFr6M0w8OucOUi7SeyKVFMDSngc2tBzoHInGm87WwmJ2DskM47
mr2MSJcr9pi261PxiQIDAQAB
-----END PUBLIC KEY-----
`

const privateKey =
`
-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKORy6rfWLJmcYDk
2BwZysFbKp7TJw69u5Ib6fPwqN3A4cZlMUJBkT2WTx/0OYtdPU0+6Raoiu1BWuv9
lCiVR50/VFU9rHApMSG5ylHURgWvozTDw65w5SLtJ7IpUUwNKeBza0HOgcicabzt
bCYnYOyQzjuavYxIlyv2mLbrU/GJAgMBAAECgYEAhm+F3WLEG5xnHXD4zd/E7TZo
yTDyoQL/7w5SMtRRdjDZX5475iDNprirP7ASu6BeieZy3NSZmnZkrCE2bESFHj/H
V2bkKJ7OqIQ3H8w7zuYeW8IA7GQSwgZ77qqNgUzjNPuu8MvAOSHHv6DW4aKy7f/H
a8pFsd1R9xnwfJT2roECQQDY0N+LQ4zeuK2ejubo91nHsd3OlZAaSYF6L3FUY0Kz
zjIDsiqDoBrnvOZX1qSiBAUzR6/qJ6H75UGKeQMRt7INAkEAwSFxmVYlPpK60CQX
XpwlEm0NpnP40z8/O7InoW27qM1RvqznGzKQwwFQ0jxVVQ29QzBVYwOqB1NCO+BT
0cMqbQJAUM/RckvUNrNo6v9ia5S+HuNZxAUU9+dL3inOuOBiXThOhfgjRnonbRaI
WFcFd+m9snZWyBTLmGLDwgQGs9TfBQJAZEtr5Qd5m/8zCLhVcoqALgt4FUMQqxOj
UOr57blAxS6rDuxXsCYYS0y5nhQ69EqISQYmsZ7Lg7ZXJSUgIkTKGQJBALZkYHhc
/lduWOaS/1lWts2uore+2MVxqdgKHVy9mGntVKW52dTUrfUKPPW+zBRiGJPAPq+T
C68C3PIVAjLbQTI=
-----END PRIVATE KEY-----
`

// 加密
export function encrypt(txt:string): string {
  const encryptor = new JSEncrypt()
  encryptor.setPublicKey(publicKey) // 设置公钥
  // const utf8data = encodeURIComponent(txt);
  return encryptor.encrypt(txt) as string // 对数据进行加密
}

// 解密
export function decrypt(txt:string): string {
  const encryptor = new JSEncrypt()
  encryptor.setPrivateKey(privateKey) // 设置私钥
  return encryptor.decrypt(txt) as string // 对数据进行解密
}
